﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using PetaPoco;

namespace Bootstrap.DataAccess.Model
{
    [TableName("answer_entity")]
    [PrimaryKey("AE_ID")]
    public class Answer
    {
        [Column]
        public string AE_ID { get; set; }
        [Column]
        public string IP { get; set; }
        public string M_ID { get; set; }
        public string A_ID { get; set; }
        public string IsFinished { get; set; }
        public string AnswerDate { get; set; }
        public string Email { get; set; }
        public string Phone { get; set; }
        public string vFlag { get; set; }
        public string vUser { get; set; }
        public string SurvryCode { get; set; }
        public string nFile { get; set; }
        public string vFlag2 { get; set; }
        public string CusName { get; set; }

        public virtual IEnumerable<Answer> Retrieves() =>
            DbManager.Create().Fetch<Answer>("select * from answer_entity");


        /// <summary>
        /// 
        /// </summary>
        /// <param name="an"></param>
        /// <returns></returns>
        public virtual bool Save(Answer an)
        {
            var ret = false;

            var db = DbManager.Create();
            try
            {
                if (an.AE_ID == "")
                    db.Insert(an);
                else
                    db.Save(an);

                ret = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return ret;
        }

        /// <summary>
        ///  
        /// </summary>
        /// <param name="value"></param>
        public virtual bool Delete(IEnumerable<string> value)
        {
            if (!value.Any()) return true;
            bool ret = false;
            var db = DbManager.Create();
            try
            {
                var ids = string.Join(",", value);


                db.Execute($"delete from answer_entity where AE_ID in ({ids})");

                ret = true;
            }
            catch (Exception ex)
            {

                throw ex;
            }
            return ret;
        }
    }
}
